package com.cmcc.passenger.ui.fragment

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import com.cmcc.passenger.R
import com.cmcc.passenger.databinding.DialogTaxiCancelBinding
import com.cmcc.shared.utils.clickWithTrigger

class AlertDialogFragment : DialogFragment() {

    private lateinit var binding: DialogTaxiCancelBinding

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        if (context == null || !isAdded) return super.onCreateDialog(savedInstanceState)
        binding = DialogTaxiCancelBinding.inflate(LayoutInflater.from(context), null, false)
//        val inflater = LayoutInflater.from(context)
        val dialog = Dialog(context!!, R.style.TransparentStyleBottomDialog)
        dialog.setContentView(binding.root)

        val m = activity!!.windowManager
        val d = m.defaultDisplay
        val p = dialog.window!!.attributes
        p.width = (d.width * 0.9).toInt()
        dialog.window!!.attributes = p
        binding.navigationButton.clickWithTrigger { dismiss() }
        binding.positiveButton.clickWithTrigger {
            Toast.makeText(
                context,
                "You are Good!!",
                Toast.LENGTH_SHORT
            ).show()
            dismiss()
        }
        return dialog
    }

    fun setContent(content: String) {
        binding.content.text = content
    }

    fun setNavigation(text: String, listener: View.OnClickListener?) {
        binding.navigationButton.text = text
        binding.navigationButton.clickWithTrigger { view ->
            run {
                listener?.onClick(view)
                dismiss()
            }
        }
    }

    fun setPositive(text: String, listener: View.OnClickListener?) {
        binding.positiveButton.text = text
        binding.navigationButton.clickWithTrigger { view ->
            run {
                listener?.onClick(view)
                dismiss()
            }
        }
    }

    class Builder {

        fun setContent(content: String) {

        }

        fun build() {}
    }

    companion object {

        fun newFragment(): AlertDialogFragment {
            return AlertDialogFragment()
        }
    }

}